package cn.stylefeng.guns.modular.transport.utils;

import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONUtil;
import cn.stylefeng.guns.modular.agv.model.params.XlbClientParamVo;
import cn.stylefeng.guns.modular.agv.model.result.XlbClientRS;
import cn.stylefeng.guns.modular.transport.entity.FindPrePickOrder;
import cn.stylefeng.guns.modular.transport.model.PlatooncarVo;
import cn.stylefeng.roses.core.util.ToolUtil;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class XlbApiUtils {

    public static Map<String, PlatooncarVo> findPrePickOrder(String hxlApiUrl,List<String> client_ids, String token) {
        Map<String, PlatooncarVo> rs = new HashMap<>();
        if (client_ids.size() > 0) {
            //查询新零帮待拣货、待质检单量
            String url = hxlApiUrl + "/hxl.open.wms.findPrePickOrder";
            JSONObject json = new JSONObject();
            json.put("client_ids", client_ids);
            String post = HttpRequest.post(url)
                    .header("Authorization", "bearer " + token).body(json.toString()).execute().body();
            try {
                JSONObject jsonObject = new JSONObject(post);
                if (jsonObject.get("code").toString().equals("0")) {
                    JSONArray jsonArray = new JSONArray(jsonObject.get("data").toString());
                    PlatooncarVo platooncarVo = null;
                    for (Object object : jsonArray) {
                        JSONObject jsonObject2 = new JSONObject(object.toString());
                        FindPrePickOrder findPrePickOrder = new FindPrePickOrder();
                        String client_id = jsonObject2.get("client_id").toString();
                        if (ToolUtil.isNotEmpty(client_id) && !client_id.equals("null")) {//门店id
                            findPrePickOrder.setClientId(client_id);
                        } else {
                            continue;
                        }
                        platooncarVo = new PlatooncarVo();
                        String pre_pick_order_count = jsonObject2.get("pre_pick_order_count").toString();
                        if (ToolUtil.isNotEmpty(pre_pick_order_count) && !pre_pick_order_count.equals("null")) {//待拣货单量
                            platooncarVo.setPrePickOrderCount(Integer.valueOf(pre_pick_order_count));
                        }

                        String pre_check_order_count = jsonObject2.get("pre_check_order_count").toString();
                        if (ToolUtil.isNotEmpty(pre_check_order_count) && !pre_check_order_count.equals("null")) {//待质检数量
                            platooncarVo.setPreCheckOrderCount(Integer.valueOf(pre_check_order_count));
                        }
                        rs.put(client_id, platooncarVo);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return rs;
    }


    /**
     * 根据巷道号获取对应门店
     * @param hxlApiUrl
     * @param param
     * @param token
     * @return
     */
    public static XlbClientRS getClientInfo(String hxlApiUrl, XlbClientParamVo param, String token) {
        Map<String, PlatooncarVo> rs = new HashMap<>();
            //查询新零帮待拣货、待质检单量
            String url = hxlApiUrl + "/hxl.open.wms.distribute.client.find";
            JSONObject json = new JSONObject();
            json.put("channel_names", param.getChannelNames());
            json.put("store_id", param.getStoreId());
            json.put("storehouse_id", param.getStorehouseId());
            String post = HttpRequest.post(url)
                    .header("Authorization", "bearer " + token).body(json.toString()).execute().body();
        return JSONUtil.toBean(post, XlbClientRS.class);
    }

}
